package com.zfl.thread.day01;

import lombok.AllArgsConstructor;

import java.util.concurrent.FutureTask;

//资源类
@AllArgsConstructor
class BankCountTread {
    private int money;

    public void withdraw(int money) {
        this.money -= money;
        System.out.println(Thread.currentThread().getName()+"取"+money+"，还剩"+this.money+"元");
    }

    public void deposit(int money) {
        this.money += money;
        System.out.println(Thread.currentThread().getName()+"存"+money+"，余额"+this.money+"元");
    }
}
public class BankCallableDemo {

    /**
     * 每一个线程不同的Callable逻辑（存款/取款）
     *  每一个Future对应一个Callable
     *  不同的Callable享用同一个资源类
     *
     */

    public static void main(String[] args) {
        BankCountTread bankCountThread = new BankCountTread(1000);
        FutureTask<Integer> futureTask1 = new FutureTask<>(()->{
            for (int i = 0; i < 20; i++)
                bankCountThread.withdraw(100);
            return null;
        });
        new Thread(futureTask1,"取款线程").start();
        new Thread(new FutureTask<Integer>(()->{
            for (int i = 0; i < 20; i++) {
                bankCountThread.deposit(100);
            }
            return null;
        }),"存款线程").start();
    }
}
